package com.syq.face;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.syq.face.fragment.RootFragment;
import com.syq.face.utils.FileUtils;
import com.syq.face.utils.RetrofitUtil;

import java.io.File;
import java.util.List;

import me.yokeyword.fragmentation.SupportActivity;
import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends SupportActivity {

    public static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RootFragment rootFragment = RootFragment.newInstance();
        MyApplication.setRootFragment(rootFragment);
        loadRootFragment(R.id.activity_main_root, rootFragment);
//        //初始化fragmentmanager
//        fragmentManager = getSupportFragmentManager();
//        init();

    }

//    private void initView() {
//        tabLayout = findViewById(R.id.activity_main_tl);
//        viewpager = findViewById(R.id.activity_main_vp2);
//    }
//
//    private void init() {
//        initView();
//        fragments.add(GroupFragment.newInstance("", "username"));
//        fragments.add(new MyFragment());
//        initViewPager();
//    }
//
//    private void initViewPager() {
//        viewpager.setAdapter(new FragmentStateAdapter(fragmentManager, getLifecycle()) {
//            @NonNull
//            @Override
//            public Fragment createFragment(int position) {
//                return fragments.get(position);
//            }
//
//            @Override
//            public int getItemCount() {
//                return fragments.size();
//            }
//        });
//        //将tabLayout与viewpager进行绑定
//        new TabLayoutMediator(tabLayout, viewpager, false, new TabLayoutMediator.TabConfigurationStrategy() {
//            @Override
//            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
//                Log.d(TAG, "onConfigureTab: ");
//                tab.setText(tabTexts[position]);
//                tab.setIcon(tabIcons[position]);
//                fragments.get(position).notifyDataReload();
//
//            }
//        }).attach();
//        viewpager.setCurrentItem(0);
//
//    }

    private void testFileUpload(Uri fileUri) {
        //获取文件
        File uploadFile = FileUtils.uriToFile(this, fileUri, null);
        Log.d(TAG, "onActivityResult: file:" + uploadFile.getAbsolutePath());
        //转换成上传的类
        MultipartBody.Part fileBody = RetrofitUtil.fileParseToMultipartBody("file", uploadFile);
        //个人需要的参数，用来指定服务器保存文件的路径 D://test/1.jpg
        String fileDirectory = "test";
        Log.d(TAG, "onActivityResult: fileDir:" + fileDirectory);
        RetrofitUtil.getInstance().getFileService()
                .uploadFile(fileDirectory, fileBody)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        // 返回值是服务器存储该文件的地址
                        Log.d(TAG, "onResponse: " + response.body());
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        Log.d(TAG, "onFailure: ");
                    }
                });
    }


    private void testFilesUpload(List<Uri> uris) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        for (Uri uri : uris) {
            //context 在fragment中用getContext()获取
            File uploadFile = FileUtils.uriToFile(this, uri, null);
            //添加要上传的所有文件
            builder.addFormDataPart("files", uploadFile.getName(), RetrofitUtil.fileParseToBody(uploadFile));
            Log.d(TAG, "onActivityResult: file:" + uploadFile.getAbsolutePath());
        }
        List<MultipartBody.Part> files = builder.build().parts();
        String fileDirectory = "test";
        Log.d(TAG, "onActivityResult: fileDir:" + fileDirectory);
        RetrofitUtil.getInstance().getFileService().uploadFiles(fileDirectory, files)
                .enqueue(new Callback<List<String>>() {
                    @Override
                    public void onResponse(Call<List<String>> call, Response<List<String>> response) {
                        Log.d(TAG, "onResponse: " + response.body().toString());
                        Log.d(TAG, "onResponse: -------------------------------");
                        String json = new Gson().toJson(response.body());
                        Log.d(TAG, "onResponse: json:" + json);
                        Log.d(TAG, "onResponse: json size:" + json.length());
                    }

                    @Override
                    public void onFailure(Call<List<String>> call, Throwable t) {

                    }
                });

    }


}